class LoggerClass {
  static LEVELS = {
    DEBUG: 0,
    INFO: 1,
    WARN: 2,
    ERROR: 3,
    FATAL: 4,
  }
  minLevel: number
  constructor(minLevel = LoggerClass.LEVELS.INFO) {
    this.minLevel = minLevel
  }
  log(level, message, ...args) {
    if (level >= this.minLevel) {
      const timestamp = new Date().toISOString()
      const levelName = Object.keys(LoggerClass.LEVELS).find(
        (key: string) => LoggerClass.LEVELS[key] === level,
      )
      switch (level) {
        case LoggerClass.LEVELS.DEBUG:
          console.debug(`{${timestamp}} ${levelName}: `, message, ...args)
          break
        case LoggerClass.LEVELS.INFO:
          console.info(`{${timestamp}} ${levelName}: `, message, ...args)
          break
        case LoggerClass.LEVELS.WARN:
          console.warn(`{${timestamp}} ${levelName}: `, message, ...args)
          break
        case LoggerClass.LEVELS.ERROR:
          console.error(`{${timestamp}} ${levelName}: `, message, ...args)
          break
        case LoggerClass.LEVELS.FATAL:
          console.error(`{${timestamp}} ${levelName}: `, message, ...args)
          break
        default:
          break
      }
      // 严重错误需要上报
      if (level >= LoggerClass.LEVELS.ERROR) {
        this.report({
          level: levelName,
          message,
          timestamp,
          details: args,
        })
      }
    }
  }
  report(errorInfo) {
    // 发送到错误信息到追踪服务器
    console.log('上报错误', errorInfo)
  }
}
const Logger = {
  $logger: new LoggerClass(),
  LEVELS: LoggerClass.LEVELS,
}
export default Logger
